﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Jb.Domain;

namespace Jb.Web.Utility
{
    public static class PackageInfo
    {
        /// <summary>
        /// Determines whether currently logged in Employer can Download resume or not
        /// </summary>
        public static bool CanDownloadResume
        {
            get
            {
                if (UserInfo.IsCompanyUser)
                {
                    var packageSubscription = SessionCache.CurrentUser.EmployerInfo.Subscription;
                    if (packageSubscription != null)
                    {
                        if (packageSubscription.SubscribedOn.AddDays(packageSubscription.Duration) >= DateTime.Now)
                        {
                            if (packageSubscription.MaxResumeDownload == -1 || packageSubscription.ResumeDownloaded < packageSubscription.MaxResumeDownload)
                                return true;
                        }
                    }
                }
                return false;
            }
        }

        ///// <summary>
        ///// Determines whether the currently logged in Employer can post a job or not
        ///// </summary>
        public static bool CanPostJob
        {
            get
            {
                if (UserInfo.IsCompanyUser)
                {
                    var packageSubscription = SessionCache.CurrentUser.EmployerInfo.Subscription;
                    if (packageSubscription != null)
                    {
                        if (packageSubscription.SubscribedOn.AddDays(packageSubscription.Duration) >= DateTime.Now)
                        {                            
                            if (packageSubscription.MaxJobPost == -1 || packageSubscription.JobPosted < packageSubscription.MaxJobPost)
                                return true;
                        }
                    }
                }
                return false;
            }
        }

        /// <summary>
        /// Gets currently subscribed package
        /// </summary>
        /// <returns></returns>
        public static PackageSubscription GetSubscribedPackage()
        {
            JbEntities context = new JbEntities();
            PackageSubscription subscribedPackage = null;
            List<PackageSubscription> packageSubscriptions = (from P in context.PackageSubscriptions.Include("Package") where P.CompanyID == SessionCache.CurrentUser.Contact.CompanyID && P.IsActive select P).ToList();
            foreach (PackageSubscription subscription in packageSubscriptions)
            {
                if (subscription.SubscribedOn.AddDays(subscription.Duration) < DateTime.Now)
                {
                    subscription.IsActive = false;
                    context.SaveChanges();
                }
                else
                    subscribedPackage = subscription;
            }
            if (subscribedPackage != null)
            {
                subscribedPackage.JobPosted = (from P in context.SubscriptionHistories where P.PackageID == subscribedPackage.PackageID && P.CompanyID == subscribedPackage.CompanyID && P.JobID != null select P.ID).Count();
                subscribedPackage.ResumeDownloaded = (from P in context.SubscriptionHistories where P.PackageID == subscribedPackage.PackageID && P.CompanyID == subscribedPackage.CompanyID && P.ResumeID != null select P.ID).Count();
            }
            return subscribedPackage;
        }
    }
}